/*
 * Copyright (C) 2015-2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


#include "PlanetWars2dRT-core/specific/model/universe_planets.h"
#include "PlanetWars2dRT-core/specific/model/universe_observer.h"
#include "PlanetWars2dRT-core/specific/model/universe_observers.h"
#include "PlanetWars2dRT-core/utils/collision_2d_uint.h"


EXTERN_C_BEGIN


void
universe_notify_nb_planets_changed(universe_t* a_universe)
{
  struct observer_data_with_code data;
  data.code = UNIVERSE_OBSERVER_CODE_NB_PLANETS_CHANGED;
  data.data = NULL;
  universe_notify_observers(a_universe, &data);
}


void
universe_add_planet_no_check_no_notify(universe_t*     a_universe,
				       const planet_t* planet_to_add)
{
  VECTOR_GENERIC_STATIC_PUSH_BACK(&a_universe->planets, *planet_to_add, planet_t);
}

void
universe_add_planet_no_check(universe_t*     a_universe,
			     const planet_t* planet_to_add)
{
  universe_add_planet_no_check_no_notify(a_universe, planet_to_add);
  universe_notify_nb_planets_changed(a_universe);
}

bool
universe_add_planet_unsafe_no_notify(universe_t*     a_universe,
				     const planet_t* planet_to_add)
{
  if(!is_rectangle_in_size_2d_uint(&a_universe->size,
				   &planet_to_add->position,
				   &planet_to_add->size))
    {
      return false;
    }
  
  const size_t nb = universe_get_nb_planets(a_universe);
  for(size_t i=0; i < nb; ++i)
    {
      const planet_t* planet_i = universe_get_const_planet(a_universe, i);
      if(collision_2d_rectangle_rectangle_uint(&planet_to_add->position,
					       &planet_i->position,
					       &planet_to_add->size,
					       &planet_i->size))
	{
	  return false;
	}
    }
  
  universe_add_planet_no_check_no_notify(a_universe, planet_to_add);
  return true;
}

bool
universe_add_planet_unsafe(universe_t*     a_universe,
			   const planet_t* planet_to_add)
{
  if(universe_add_planet_unsafe_no_notify(a_universe, planet_to_add))
    {
      universe_notify_nb_planets_changed(a_universe);
      return true;
    }
  return false;
}

bool
universe_add_planet_no_notify(universe_t*     a_universe,
			      const planet_t* planet_to_add)
{
  return
    a_universe == NULL || planet_to_add == NULL
    ? false
    : universe_add_planet_unsafe_no_notify(a_universe, planet_to_add);
}

bool
universe_add_planet(universe_t*     a_universe,
		    const planet_t* planet_to_add)
{
  if(universe_add_planet_no_notify(a_universe, planet_to_add))
    {
      universe_notify_nb_planets_changed(a_universe);
      return true;
    }
  return false;
}


void
universe_remove_planets_no_notify_unsafe(universe_t* a_universe)
{
      const size_t nb = universe_get_nb_planets_unsafe(a_universe);
      for(size_t i=0; i < nb; ++i)
	{
	  planet_destructor(universe_get_planet_no_check(a_universe, i));
	}
      VECTOR_GENERIC_STATIC_CLEAR(&a_universe->planets);
}

void
universe_remove_planets_no_notify(universe_t* a_universe)
{
  if(a_universe != NULL)
    {
      universe_remove_planets_no_notify_unsafe(a_universe);
    }
}

void
universe_remove_planets(universe_t* a_universe)
{
  if(universe_get_nb_planets(a_universe) > 0)
    {
      universe_remove_planets_no_notify (a_universe);
      universe_notify_nb_planets_changed(a_universe);
    }
}


EXTERN_C_END
